package com.cj.mobile.iabp.mvp.model;

import android.text.TextUtils;

import com.cj.mobile.iabp.app.global.AccountManager;
import com.cj.mobile.iabp.mvp.contract.AddRoundsInfoContract;
import com.cj.mobile.iabp.mvp.model.api.service.AccountService;
import com.cj.mobile.iabp.mvp.model.entity.DictInfo;
import com.jess.arms.cj.ApiOperator;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.mvp.BaseModel;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 2025/09/28 15:20
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class AddRoundsInfoModel extends BaseModel implements AddRoundsInfoContract.Model {
    @Inject
    ApiOperator apiOperator;                                                                        // 数据转换

    @Inject
    AccountManager mAccountManager;


    @Inject
    public AddRoundsInfoModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.apiOperator = null;
        this.mAccountManager = null;
    }

    @Override
    public Observable<List<DictInfo>> getDictList(String type) {
        Map<String, Object> params = new HashMap<>();
        params.put("type", type);

        return apiOperator.chain(params, request -> mRepositoryManager.obtainRetrofitService(AccountService.class).getDictList(mAccountManager.getToken(), request));
    }

    @Override
    public Observable<List<String>> upload(List<String> files) {

        /*图片集*/
        List<MultipartBody.Part> parts = new ArrayList<>(files.size());
        for (String val : files) {
            File file = new File(val);
            RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
            MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
            parts.add(part);
        }

        //用户登录id
        RequestBody userIdVal = RequestBody.create(MediaType.parse("text/plain"), mAccountManager.getUserid());
        return mRepositoryManager.obtainRetrofitService(AccountService.class).uploadImages(mAccountManager.getToken(), userIdVal, parts).flatMap(ApiOperator.<List<String>>transformResponse());
    }

    @Override
    public Observable<Integer> addRoundsInfo(String per, String patienId, String doctorId, String regCode, String patientName, String bedCode,
                                             String roomCode, String doctorName, String place1, String place2, String roundTime,
                                             String fbType, String gssType, String yali, String haiqi, List<String> lessL,
                                             List<String> lessR, String ecmo, String mashine, String special, String checkTime,
                                             String ast, String alt, String cr, String egfr, String myo, String clac, String ga,
                                             String leftSize, String leftScore, List<String> images) {
        Map<String, Object> params = new HashMap<>();
        params.put("per", per);
        params.put("patienId", patienId);
        params.put("doctorId", doctorId);
        params.put("regCode", regCode);
        params.put("patientName", patientName);
        params.put("bedCode", bedCode);
        params.put("roomCode", roomCode);
        params.put("doctorName", doctorName);
        params.put("place1", place1);
        params.put("place2", place2);
        params.put("roundTime", TextUtils.isEmpty(roundTime) ? null : roundTime);
        params.put("fbType", fbType);
        params.put("gssType", gssType);
        params.put("yali", yali);
        params.put("haiqi", haiqi);
        params.put("lessL", lessL);
        params.put("lessR", lessR);
        params.put("ecmo", ecmo);
        params.put("mashine", mashine);
        params.put("special", special);
        params.put("checkTime", TextUtils.isEmpty(checkTime) ? null : checkTime);
        params.put("ast", ast);
        params.put("alt", alt);
        params.put("cr", cr);
        params.put("egfr", egfr);
        params.put("myo", myo);
        params.put("clac", clac);
        params.put("ga", ga);
        params.put("leftSize", leftSize);
        params.put("leftScore", leftScore);
        params.put("images", images);

        return apiOperator.chain(params, request -> mRepositoryManager.obtainRetrofitService(AccountService.class).addRounds(mAccountManager.getToken(), request));
    }

    @Override
    public Observable<Integer> updateRounds(String per, String id, String patienId, String doctorId, String regCode, String patientName, String bedCode,
                                            String roomCode, String doctorName, String place1, String place2, String roundTime,
                                            String fbType, String gssType, String yali, String haiqi, List<String> lessL,
                                            List<String> lessR, String ecmo, String mashine, String special, String checkTime,
                                            String ast, String alt, String cr, String egfr, String myo, String clac, String ga,
                                            String leftSize, String leftScore, List<String> images) {
        Map<String, Object> params = new HashMap<>();
        params.put("per", per);
        params.put("id", id);
        params.put("patienId", patienId);
        params.put("doctorId", doctorId);
        params.put("regCode", regCode);
        params.put("patientName", patientName);
        params.put("bedCode", bedCode);
        params.put("roomCode", roomCode);
        params.put("doctorName", doctorName);
        params.put("place1", place1);
        params.put("place2", place2);
        params.put("roundTime", TextUtils.isEmpty(roundTime) ? null : roundTime);
        params.put("fbType", fbType);
        params.put("gssType", gssType);
        params.put("yali", yali);
        params.put("haiqi", haiqi);
        params.put("lessL", lessL);
        params.put("lessR", lessR);
        params.put("ecmo", ecmo);
        params.put("mashine", mashine);
        params.put("special", special);
        params.put("checkTime", TextUtils.isEmpty(checkTime) ? null : checkTime);
        params.put("ast", ast);
        params.put("alt", alt);
        params.put("cr", cr);
        params.put("egfr", egfr);
        params.put("myo", myo);
        params.put("clac", clac);
        params.put("ga", ga);
        params.put("leftSize", leftSize);
        params.put("leftScore", leftScore);
        params.put("images", images);

        return apiOperator.chain(params, request -> mRepositoryManager.obtainRetrofitService(AccountService.class).updateRounds(mAccountManager.getToken(), request));
    }

}